#include "Collision.h"

bool Collision::Rect_Rect( RECT r1,RECT r2 )
{
	float XDistance1=abs(r2.right-r1.left);
	float XDistance2=abs(r1.right-r2.left);
	float YDistance1=abs(r2.top-r1.bottom);
	float YDistance2=abs(r1.top-r2.bottom);
	return (max(XDistance1,XDistance2)<=(r1.right-r1.left)+(r2.right-r2.left)&&max(YDistance1,YDistance2)<=(r1.bottom-r1.top)+(r2.bottom-r2.top));
}

bool Collision::Rect_Circle( RECT r,float x,float y,float R )
{
	float Mx=middle((float)r.left,(float)r.right,x);
	float My=middle((float)r.top,(float)r.bottom,y);	
	return (Distance(Mx,My,x,y)<=R);
}

bool Collision::Circle_Circle( float x1,float y1,float R1,float x2,float y2,float R2 )
{
	return Distance(x1,y1,x2,y2)<=(R1+R2);
}

bool Collision::Point_Line( float xM,float yM,float xA,float yA,float xB,float yB )
{
	return (Distance(xA,yA,xM,yM)+Distance(xM,yM,xB,yB)==Distance(xA,yA,xB,yB));
}

float Collision::Distance( float xA,float yA,float xB,float yB )
{
	return sqrt((xB-xA)*(xB-xA)+(yB-yA)*(yB-yA));
}

float Collision::middle( float a,float b,float c )
{
	if (a>=b)
	{
		if (b>=c)
		{
			return b;
		}
		else
		{
			if (c>=a)
			{
				return a;
			} 
			else
			{
				return c;
			}
		}
	}
	else//a<=b
	{
		if (c<=a)
		{
			return a;
		} 
		else
		{
			if (c>=b)
			{
				return b;
			} 
			else
			{
				return c;
			}
		}
	}
}
